package cn.edu.ldu.controller;

import cn.edu.ldu.entity.FriendsEntity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class FriendController {
    private static FriendsEntity friendsEntity;
    private static List<FriendsEntity> friendsList;
    private static List<FriendsEntity> friendOnline;

    public static FriendsEntity getFriendsEntity(int i) {
        return friendOnline.get(i);
    }
    public static FriendsEntity getFriendsEntity(Long friendId) {
        for(FriendsEntity fe : friendsList){
            if(fe.getFriendId().equals(friendId))
                return fe;
        }

        return null;
    }

    public static List<FriendsEntity> getFriendsList(){return friendsList;}

    public static List<FriendsEntity> getOnlineFriendsEntity() {
        return friendOnline;
    }

    public static void setFriendsEntity(FriendsEntity friendsEntity) {
        FriendController.friendsEntity = friendsEntity;
    }


    /**
     * 总的好友
     * @param friendsEntity
     */
    public static void addToList(FriendsEntity friendsEntity){
        if(friendsList == null)
            friendsList = new ArrayList<>();
        friendsList.add(friendsEntity);
    }

    /**
     * 在线好友
     * @param friendsEntity
     */
    public static void addOnline(FriendsEntity friendsEntity){
        if (friendOnline==null)
            friendOnline = new ArrayList<>();
        friendOnline.add(friendsEntity);
    }

    /**
     * 好友离线
     * @param friendsEntity
     */
    public static void deleteOnline(FriendsEntity friendsEntity){
        int z=0;
        for(int i=0;i<friendOnline.size();i++){
            if(friendOnline.get(i).getFriendId().equals(friendsEntity.getId())){
                z=i;
            }
        }
        friendOnline.remove(z);
    }

    /**
     * 创建好友列表
     * @return
     */
    public static List<FriendsEntity> friendsList(){
        if(friendsList.size()==0){  return null; }
        List<FriendsEntity> newFriendlist = new ArrayList<>();
        newFriendlist.set(0, friendsList.get(0));
        int index=1;
        for(int i=1;i<friendsList.size();i++){
            int j=0;
            for(;j<index;j++){
                if(newFriendlist.get(j).equals(friendsList.get(i))){
                    break;
                }
            }
            if (j==index){
                newFriendlist.set(index++,friendsList.get(i));
            }
        }
        return newFriendlist;
    }

}
